﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Tools;
using Halcon二次开发.View.模板匹配;
using Halcon二次开发.View.距离转换工具;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.距离转换工具
{
    public class DistanceToolController
    {
        /// <summary>
        /// 距离转换界面
        /// </summary>
        DistanceToolForm distanceToolForm;

        /// <summary>
        /// 控件句柄
        /// </summary>
        HWindow hWindon;


        #region 输入输出参数
        public double[] IntputDistance;

        public double[] OutputDistance;
        #endregion

        public DistanceToolController(DistanceToolForm distanceToolForm)
        {
            //获取当前工位
            hWindon = StaicResouce.HwindowFlowNodeUrse.hSmartWindowControl1.HalconWindow;
            this.distanceToolForm = distanceToolForm;
        }

        /// <summary>
        /// 运行
        /// </summary>
        public void Run() {
            distanceToolForm.textBox4.Text = "";
            try
            {
                if (IntputDistance!=null)
                {
                    if (IntputDistance.Length > 0)
                    {
                        distanceToolForm.dataGridView1.Rows.Clear();
                       OutputDistance = new double[IntputDistance.Length];
                        for (int i = 0; i < IntputDistance.Length; i++)
                        {
                            distanceToolForm.textBox5.Text = IntputDistance[i].ToString();

                            double Sum1 = Convert.ToInt16(distanceToolForm.textBox1.Text.Trim());
                            double Sum2 = Convert.ToInt16(distanceToolForm.textBox2.Text.Trim());
                            double Sum3 = Sum2 / Sum1;

                            distanceToolForm.textBox4.Text = Sum3.ToString();
                            distanceToolForm.textBox3.Text = (IntputDistance[i] * Sum3).ToString();

                            OutputDistance[i] = IntputDistance[i] * Sum3;
                            distanceToolForm.dataGridView1.Rows.Add(IntputDistance[i], OutputDistance[i]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                OutputDistance = new double[0];
                //MessageBox.Show("运算错误：" + ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw new Exception("运算错误：" + ex.Message);
            }
        }
    }
}
